<?php

namespace app\admin\controller;

/**
 *
 * 系统设置，SEO
 * Class System
 * @package app\admin\controller
 */
class System extends base
{

    /**
     * 列表页
     */
    public function index()
    {
        $type = $this->request->param('type');
        $data = cache('system'.$type);

        if(empty($data)){
            $data= $this->model->getnoteall($type);
            cache('system'.$type,$data);
        }

        $this->assign('data', $data);
        return $this->fetch($type . '/index');
    }


    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }

        $data = $this->request->post();
        $cheak = [];

        $cheak = $this->model->getnoteall($data['type']);
        foreach ($data as $key => $value) {
            if ($key != 'type' && $cheak[$key]['value'] != $value) {
                if (file_exists('./uploads/image/' . $cheak[$key]['value']) && $cheak[$key]['value']) {
                    unlink('./uploads/image/' . $cheak[$key]['value']);
                }
                $this->model->save(['value' => html_entity_decode($value)], ['title' => $key, 'type' => $data['type']]);
            }
        }
        cache('system'.$data['type'],null);
        return ajax_info('修改成功', 'u');
    }
}
